﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMP_QuanLySV.BLL;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_ThemSinhVien : UserControl
    {
        Form parent;
        BLLSinhVien BL = new BLLSinhVien();
        BLLDanToc DT = new BLLDanToc();
        BLLTonGiao TG = new BLLTonGiao();

        // constructor
        public UC_ThemSinhVien(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        // cho thuoc tinh text cua cac textboxes tro thanh ""
        private void clearTextboxes()
        {
            txtDiaChi.Text= "";
            txtDienThoai.Text = "";
            txtGhiChu.Text = "";
            txtEmail.Text = "";
            txtHoLot.Text = "";
            txtHoTenCha.Text = "";
            txtHoTenMe.Text = "";
            txtMASV.Text = "";
            txtNamTNPT.Text = "";
            txtNgaySinh.Text = "";
            txtNgheNghiepCha.Text = "";
            txtNgheNghiepMe.Text = "";
            txtNoiSinh.Text = "";
            txtQuocTich.Text = "";
            txtTen.Text = "";
        }

        // set thuoc tinh readOnly/enabled cho tat ca cac controls ve 1 gia tri
        void setReadOnlyAll(bool val)
        {
            txtMASV.ReadOnly= val;
            txtHoLot.ReadOnly = val;
            txtTen.ReadOnly = val;
            txtDiaChi.ReadOnly = val;
            txtDienThoai.ReadOnly = val;
            txtEmail.ReadOnly = val;
            txtHoTenCha.ReadOnly = val;
            txtHoTenMe.ReadOnly = val;
            txtNamTNPT.ReadOnly = val;
            txtNgaySinh.ReadOnly = val;
            txtNgheNghiepCha.ReadOnly = val;
            txtNgheNghiepMe.ReadOnly = val;
            txtNoiSinh.ReadOnly = val;
            txtQuocTich.ReadOnly = val;
            txtGhiChu.ReadOnly = val;
        }

        void setBtnEnabled(bool val)
        {
            btnLuu.Enabled = val;
            btnHuy.Enabled = val;
        }   

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            setReadOnlyAll(false);
            setBtnEnabled(true);
            btnThemMoi.Enabled = false;

            // set trang thai ve che do them moi
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai combobox va buttons
            setReadOnlyAll(true);
            cbxDanToc.DataSource = BL.returnListSinhVien();
            setBtnEnabled(false);
            btnThemMoi.Enabled = true;
            UC_ThemSinhVien_Load(sender, e);            
        }

        private void UC_ThemSinhVien_Load(object sender, EventArgs e)
        {
            setBtnEnabled(false);
            // nap du lieu cho combobox
           
            cbxDanToc.DataSource = DT.returnListDanToc();
            cbxDanToc.DisplayMember = "TenDanToc";
            cbxDanToc.ValueMember = "DanTocID";
            cbxDanToc.SelectedIndex = 0;


            cbxTonGiao.DataSource = TG.returnListTonGiao();
            cbxTonGiao.DisplayMember = "TenTonGiao";
            cbxTonGiao.ValueMember = "TonGiaoID";
            cbxTonGiao.SelectedIndex = 0;
            // mac dinh dong dau tien trong combobox duoc chon (selected)

            setReadOnlyAll(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            // disable toan bo cac textboxes
            setReadOnlyAll(true);

            // kiem tra tinh hop le cua du lieu nhap vao
            SinhVien S = new SinhVien();
            S.DiaChi = txtDiaChi.Text;
            S.DienThoai = txtDienThoai.Text;
            S.Email = txtEmail.Text;
            S.GhiChu = txtGhiChu.Text;
            S.HoLot = txtHoLot.Text;
            S.HoTenCha = txtHoTenCha.Text;
            S.HoTenMe = txtHoTenMe.Text;
            S.SVID = txtMASV.Text;
            S.Ten = txtTen.Text;
            S.NoiSinh = txtNoiSinh.Text;
            S.NgheNghiepMe = txtNgheNghiepMe.Text;
            S.NgheNghiepCha = txtNgheNghiepCha.Text;
            S.NgaySinh = txtNgaySinh.Text;
            S.NamTNPT = txtNamTNPT.Text;
            S.QuocTich = txtQuocTich.Text;


            if (BL.kiemTra(S))
            {
                // update csdl 
                
                BL.insertSinhVien(S);

                // disable cac nut Luu va Huy
                setBtnEnabled(false);
                btnThemMoi.Enabled = true;
                clearTextboxes();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
            UC_ThemSinhVien_Load(sender, e);
        }

    }
}
